<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <link href="https://gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.80.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>kotlin-String &middot; K J</title>
  <meta name="description" content="" />

  
  <link type="text/css" rel="stylesheet" href="http://kartjim.gitee.io/hugo/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="http://kartjim.gitee.io/hugo/css/poole.css">
  <link type="text/css" rel="stylesheet" href="http://kartjim.gitee.io/hugo/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="http://kartjim.gitee.io/hugo/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="icon" href="/hugo/favicon.png">
  
  
</head>

  <body class="theme-base-09 ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="http://kartjim.gitee.io/hugo/"><h1>K J</h1></a>
      <p class="lead">
       kart jim`blog 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="http://kartjim.gitee.io/hugo/">Home</a> </li>
        <li><a href="https://blog.csdn.net/qq_46590483?spm=1001.2100.3001.5343"> CSDN </a></li><li><a href="https://github.com/can-dy-jack"> Github </a></li><li><a href="https://www.cnblogs.com/kartjim"> 博客园 </a></li>
      </ul>
    </nav>

    <p>Copyright 2021 © kart jim</p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>kotlin-String</h1>
  <time datetime=2020-12-11T22:41:05Z class="post-date">Fri, Dec 11, 2020</time>
  <h1 id="kotlin---字符串">Kotlin - 字符串</h1>
<p>字符串只是一个字符串序列，包含在双引号中。在 Kotlin 中，字符串由类型 <code>String</code> 表示。它是实践中使用最广泛的数据类型之一。</p>
<h4 id="1-声明">1. 声明</h4>
<p>声明字符串变量：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> str: String = <span style="color:#e6db74">&#34;Hello Kotlin&#34;</span>
</code></pre></div><p>当然，因为 Kotlin 出色的类型推断，您常常无需表明类型：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> str = <span style="color:#e6db74">&#34;Hello Kotlin&#34;</span>
</code></pre></div><h4 id="2-字符串的长度">2. 字符串的长度</h4>
<p>您可以获取访问属性的字符串中的字符数。此属性具有类型的值。<code>length</code> <code>Int</code></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> language = <span style="color:#e6db74">&#34;Kotlin&#34;</span>
println(language.length) <span style="color:#75715e">// 6
</span><span style="color:#75715e"></span>
<span style="color:#66d9ef">val</span> empty = <span style="color:#e6db74">&#34;&#34;</span>
println(empty.length) <span style="color:#75715e">// 0
</span><span style="color:#75715e"></span>
<span style="color:#75715e">// 关于汉字的长度
</span><span style="color:#75715e"></span><span style="color:#66d9ef">val</span> str = <span style="color:#e6db74">&#34;你好&#34;</span>
println(str.length) <span style="color:#75715e">// 2
</span></code></pre></div><h4 id="3-访问字符">3. 访问字符</h4>
<p>字符串的元素是单个字符，可以通过其索引访问。字符串的第一个元素具有索引 0 。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> greeting = <span style="color:#e6db74">&#34;Hello&#34;</span>
 
<span style="color:#66d9ef">val</span> first = greeting[<span style="color:#ae81ff">0</span>]  <span style="color:#75715e">// &#39;H&#39;
</span><span style="color:#75715e"></span><span style="color:#66d9ef">val</span> second = greeting[<span style="color:#ae81ff">1</span>] <span style="color:#75715e">// &#39;e&#39;
</span><span style="color:#75715e"></span><span style="color:#66d9ef">val</span> five = greeting[<span style="color:#ae81ff">4</span>]   <span style="color:#75715e">// &#39;o&#39;
</span></code></pre></div><p>最后一个元素的索引等于字符串的长度减去 1 。对于字符串，最后一个元素为 。其索引为 4 ，因为字符串的长度为  5 。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> last = greeting[greeting.length - <span style="color:#ae81ff">1</span>] <span style="color:#75715e">// &#39;o&#39;
</span><span style="color:#75715e"></span><span style="color:#66d9ef">val</span> prelast = greeting[greeting.length - <span style="color:#ae81ff">2</span>] <span style="color:#75715e">// &#39;l&#39;
</span></code></pre></div><p>Kotlin 提供了几种访问字符串的第一个字符和最后一个字符的便捷方法：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin">println(greeting.first())   <span style="color:#75715e">// &#39;H&#39;
</span><span style="color:#75715e"></span>println(greeting.last())    <span style="color:#75715e">// &#39;o&#39;
</span><span style="color:#75715e"></span>println(greeting.lastIndex) <span style="color:#75715e">// 4
</span></code></pre></div><p>您可以使用此功能编写更具可读性的代码。
还可以用 for 循环迭代字符串:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">for</span> (c <span style="color:#66d9ef">in</span> str) {
    println(c)
}
</code></pre></div><h4 id="4-不变性">4. 不变性</h4>
<p>字符串是不可变的，意思是，一旦创建字符串总是相同的。不能修改字符串的元素。因此，下面的示例不起作用：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> str = <span style="color:#e6db74">&#34;string&#34;</span>
str[<span style="color:#ae81ff">3</span>] = <span style="color:#e6db74">&#39;o&#39;</span> <span style="color:#75715e">// an error here!
</span></code></pre></div><p>但这个工作：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">var</span> str = <span style="color:#e6db74">&#34;string&#34;</span>
str = <span style="color:#e6db74">&#34;strong&#34;</span>
</code></pre></div><p>实际上，我们不会修改变量中的存储值。相反，我们为它分配一个新值。因此，这是绝对合法的。这是使用字符串的方法之一。如果需要修改字符串，只需创建一个新字符串。</p>
<h4 id="5-连接字符串">5. 连接字符串</h4>
<p>可以使用运算符连接两个字符串：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> str1 = <span style="color:#e6db74">&#34;ab&#34;</span>
<span style="color:#66d9ef">val</span> str2 = <span style="color:#e6db74">&#34;cde&#34;</span>
<span style="color:#66d9ef">val</span> result = str1 + str2 <span style="color:#75715e">// &#34;abcde&#34;
</span></code></pre></div><p>当我们连接两个字符串时，将创建一个新字符串（因为字符串是不可变的）。</p>
<p>可以在同一表达式中串联多个字符串：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> firstName = <span style="color:#e6db74">&#34;John&#34;</span>
<span style="color:#66d9ef">val</span> lastName = <span style="color:#e6db74">&#34;Smith&#34;</span>
<span style="color:#66d9ef">val</span> fullName = firstName + <span style="color:#e6db74">&#34; &#34;</span> + lastName
</code></pre></div><p>注意，不一样，因为串联不是换向操作（不像添加）。
将值追加到字符串
该运算符还适用于将不同类型的值附加到字符串。该值将自动转换为字符串，然后串联到目标字符串。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> str = <span style="color:#e6db74">&#34;abc&#34;</span> + <span style="color:#ae81ff">10</span> + <span style="color:#66d9ef">true</span>
println(str) <span style="color:#75715e">// abc10true
</span><span style="color:#75715e"></span>
<span style="color:#66d9ef">val</span> code = <span style="color:#e6db74">&#34;123&#34;</span> + <span style="color:#ae81ff">456</span> + <span style="color:#e6db74">&#34;789&#34;</span>
println(code) <span style="color:#75715e">// 123456789
</span></code></pre></div><p>表达式必须始终以字符串为起点，请看下面的示例。它不工作，因为第一个操作数是一个数字：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> str = <span style="color:#ae81ff">10</span> + <span style="color:#e6db74">&#34;abc&#34;</span> <span style="color:#75715e">// an error here!
</span></code></pre></div><p>下一个可能会令您大吃一惊：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> str = <span style="color:#e6db74">&#34;abc&#34;</span> + <span style="color:#ae81ff">11</span> + <span style="color:#ae81ff">22</span>
println(str) <span style="color:#75715e">// abc1122
</span></code></pre></div><p>为什么会这样？因为首先它追加到字符串，然后追加到字符串。</p>
<h4 id="6-比较字符串">6. 比较字符串</h4>
<p>要比较两个字符串，请使用（相等）和（不等于）运算符。两个运算符都使用两个字符串作为操作数，并返回类型 （或 ） 的值。</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> first = <span style="color:#e6db74">&#34;first&#34;</span>
<span style="color:#66d9ef">val</span> second = <span style="color:#e6db74">&#34;second&#34;</span>
<span style="color:#66d9ef">var</span> str = <span style="color:#e6db74">&#34;first&#34;</span>
 
println(first <span style="color:#f92672">==</span> str)  <span style="color:#75715e">// true
</span><span style="color:#75715e"></span>println(first <span style="color:#f92672">==</span> second) <span style="color:#75715e">// false
</span><span style="color:#75715e"></span>println(first <span style="color:#f92672">!=</span> second) <span style="color:#75715e">// true
</span></code></pre></div><h4 id="7-字符串模板">7. 字符串模板</h4>
<p>字符串字面值可以包含模板表达式 ，即一些小段代码，会求值并把结果合并到字符串中。 模板表达式以美元符（$）开头，由一个简单的名字构成:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> i = <span style="color:#ae81ff">10</span>
println(<span style="color:#e6db74">&#34;i = </span><span style="color:#e6db74">$i</span><span style="color:#e6db74">&#34;</span>) <span style="color:#75715e">// 输出“i = 10”
</span></code></pre></div><p>或者用花括号括起来的任意表达式:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> s = <span style="color:#e6db74">&#34;abc&#34;</span>
println(<span style="color:#e6db74">&#34;</span><span style="color:#e6db74">$s</span><span style="color:#e6db74">.length is </span><span style="color:#e6db74">${s.length}</span><span style="color:#e6db74">&#34;</span>) <span style="color:#75715e">// 输出“abc.length is 3”
</span></code></pre></div><p>原始字符串与转义字符串内部都支持模板。 如果你需要在原始字符串中表示字面值 $ 字符（它不支持反斜杠转义），你可以用下列语法：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> price = <span style="color:#e6db74">&#34;&#34;&#34;
</span><span style="color:#e6db74"></span><span style="color:#e6db74">${&#39;$&#39;}</span><span style="color:#e6db74">9.99
</span><span style="color:#e6db74">&#34;&#34;&#34;</span>
</code></pre></div><h4 id="8-其他">8. 其他</h4>
<p>原始字符串 使用三个引号（&quot;&quot;&quot;）分界符括起来，内部没有转义并且可以包含换行以及任何其他字符:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> text = <span style="color:#e6db74">&#34;&#34;&#34;
</span><span style="color:#e6db74">    for (c in &#34;foo&#34;)
</span><span style="color:#e6db74">        print(c)
</span><span style="color:#e6db74">&#34;&#34;&#34;</span>
</code></pre></div><p>你可以通过 <code>trimMargin()</code> 函数去除前导空格：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-kotlin" data-lang="kotlin"><span style="color:#66d9ef">val</span> text = <span style="color:#e6db74">&#34;&#34;&#34;
</span><span style="color:#e6db74">    |Tell me and I forget.
</span><span style="color:#e6db74">    |Teach me and I remember.
</span><span style="color:#e6db74">    |Involve me and I learn.
</span><span style="color:#e6db74">    |(Benjamin Franklin)
</span><span style="color:#e6db74">    &#34;&#34;&#34;</span>.trimMargin()
</code></pre></div><p>默认 <code>|</code> 用作边界前缀，但你可以选择其他字符并作为参数传入，比如 <code>trimMargin(&quot;&gt;&quot;)</code> 。
<a href="https://blog.csdn.net/moriafly/article/details/107165523">文章出处</a></p>
<p>*[文章出处]:moriafly</p>

</div>


    </main>

    
      
    
  </body>
</html>
